#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include <string.h>

void InitString(String *S)
{
     S->ch = NULL;
     S->length = 0;
}
int StrAssign(String *S, char *str)
{
     int len = strlen(str);
     if (S->ch != NULL) //如果字符串之前就不为空,就要释放这段内存，否则会造成内存泄漏
          free(S->ch);
     S->ch = malloc(sizeof(char) * len);
     if (S->ch == NULL)
          return -1;
     for (int i = 0; i < len; i++)
          S->ch[i] = str[i];
     S->length = len;
     return 0;
}
void StrPrint(String *S)
{
     for (int i = 0; i < S->length; i++)
          printf("%c", S->ch[i]);
     printf("\n");
}
int StrCopy(String *S, String *T)
{
     int len = StrLength(T);
     if (S->ch != NULL) //如果字符串之前就不为空,就要释放这段内存，否则会造成内存泄漏
          free(S->ch);
     S->ch = malloc(sizeof(char) * len);
     if (S->ch == NULL)
          return -1;
     for (int i = 0; i < len; i++)
          S->ch[i] = T->ch[i];
     S->length = len;
     return 0;
}
int StrCompare(String *S, String *T)
{
     if (S->length > T->length)
          return 1;
     else if (S->length < T->length)
          return -1;
     int i = 0;
     while (i < S->length)
     {
          if (S->ch[i] < T->ch[i])
               return -1;
          else if (S->ch[i] > T->ch[i])
               return 1;
          else
               i++;
     }
     return 0;
}
int StrEmpty(String *S)
{
     return S->length == 0;
}
int StrLength(String *S)
{
     return S->length;
}
int StrConcat(String *S, String *T, String *X)
{
     if (S->ch != NULL)
          free(S->ch);
     int len1 = StrLength(T);
     int len2 = StrLength(X);
     S->ch = malloc(sizeof(char) * (len1 + len2));
     if (S->ch == NULL)
          return -1;
     for (int i = 0; i < len1; i++)
          S->ch[i] = T->ch[i];
     for (int i = 0; i < len2; i++)
          S->ch[i + len1] = X->ch[i];
     S->length = len1 + len2;
     return 0;
}
int SubString(String *S, String *sub, int pos, int len)
{
     if (pos < 0 || len < 0 || len + pos > S->length)
          return -1;
     if (sub->ch != NULL)
          free(sub->ch);
     sub->ch = malloc(sizeof(char) * len);
     if (sub->ch == NULL)
          return -2;
     for (int i = 0; i < len; i++)
          sub->ch[i] = S->ch[pos + i];
     sub->length = len;
     return 0;
}
int StrInsert(String *S, int pos, String *T)
{
     int s_len = StrLength(S);
     int t_len = StrLength(T);
     if (t_len == 0)
          return -1;
     if (pos < 0 || pos > s_len)
          return -2;
     String *current = realloc(S->ch, sizeof(char) * (s_len + t_len));
     if (current == NULL)
          return -3;
     for (int i = 0; i < s_len - pos; i++)
          S->ch[pos + t_len + i] = S->ch[pos + i];
     for (int i = 0; i < t_len; i++)
          S->ch[pos + i] = T->ch[i];
     S->length += t_len;
     return 0;
}
int StrDelete(String *S, int pos, int len)
{
     int s_len = StrLength(S);
     if (pos < 0 || len < 0 || pos + len > s_len)
          return -1;
     for (int i = 0; i < len; i++)
          S->ch[pos + i] = S->ch[pos + len + i];
     S->length -= len;
     return 0;
}
void StrClean(String *S)
{
     S->length = 0;
     if (S->ch != NULL)
          free(S->ch);
     S->ch == NULL;
}